<?php

/*
 * Action - Attack 5$
 * 
 * +2 Coin
 * If there are any cards in the trash costing from 3 coin to 6 Coin, gain one of them. Otherwise, each other player reveals the top 2 cards of his deck, trashes one of them costing from 3 Coin to 6 Coin, and discards the rest.
 */

class Rogue extends Card implements iAction, iKingdom, iDarkAges, iAttack {

    public function __construct() {
        parent::__construct("Rogue", 5);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
            "+2 Coin",
            "If there are any cards in the trash costing from 3 coin to 6 Coin, gain one of them. Otherwise, each other player reveals the top 2 cards of his deck, trashes one of them costing from 3 Coin to 6 Coin, and discards the rest.");
    }

    public function play(Game $game, $isRealCard) {
        $game->addMoney(new Price(2));
        $game->attack($this);

        $trash = $game->getTrash();
        $trash_option = array();
        foreach ($trash as $card) {
            $coins = $card->getPrice($game)->getCoins();
            if ($coins >= 3 && $coins <= 6) {
                $trash_option[] = $card;
            }
        }

        if (sizeof($trash_option) == 1) {
            $card = $game->moveCardFromPileToPile($trash_option[0], Trash, DiscardPile);
            ScreenDisplay::addScreenMessage($game, true, "gain " . nicePrint($card) . " from trash", " gains " . ScreenDisplay::$LMSG . " from trash");
        } elseif (sizeof($trash_option) > 1) {
            $output = MySocket::GetUserReply($game, Input_ChooseOne, $trash_option, 1, 1, null, "Please choose trashed card to gain");
            $card = $game->moveCardFromPileToPile($output[0], Trash, DiscardPile);
            ScreenDisplay::addScreenMessage($game, true, "gain " . nicePrint($card) . " from trash", " gains " . ScreenDisplay::$LMSG . " from trash");
        } else {
            $game->setPhase(Phase_Sleep);
            $names = $game->getOtherPlayersNames(true);

            foreach ($names as $name) {
                $cards = $game->removeCardsFromDrawDeck(2, $name);

                ScreenDisplay::addScreenMessage($game, true, " reveal " . nicePrint($cards), " reveals " . ScreenDisplay::$LMSG, $name);

                $has_trashable_cards = false;
                $trashable_cards = array();
                foreach ($cards as $card) {
                    $coins = $card->getPrice($game)->getCoins();
                    if ($coins >= 3 && $coins <= 6) {
                        $has_trashable_cards = true;
                        $trashable_cards[] = $card;
                    } else {
                        $game->putCardInDiscardDeck($card, $name);
                    }
                }

                $trashed_card = null;
                //remember that the other player treasure cards are currently in the 'air' we should decide where to put them
                if (sizeof($trashable_cards) == 2) {

                    $output = MySocket::GetUserReply($game, Input_ChooseOne, $trashable_cards, 1, 1, null, "Choose a card to trash", $name);


                    $trashed_card = $output[0];
                    $trashed_card = n2c($trashed_card);

                    $card1_name = $trashable_cards[0];
                    $card2_name = $trashable_cards[1];
                    $other_card = null;
                    if ($card1_name->getKey() == $trashed_card->getKey()) {
                        $other_card = $card2_name;
                    } else if ($card2_name->getKey() == $trashed_card->getKey()) {
                        $other_card = $card1_name;
                    }
                    //we discard the not chosen card
                    $game->putCardInDiscardDeck($other_card, $name);
                } else if (sizeof($trashable_cards) == 1) {
                    $trashed_card = $trashable_cards[0];
                }

                if ($trashed_card) {
                    //trashing the coosen card
                    $game->putCardInTrash($trashed_card);
                    ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($trashed_card), " trashes " . ScreenDisplay::$LMSG, $name);
                }
            }
        }
        $game->cardActionFinished();
        return true;
    }

}

?>